(function($){
	var KEY = "jforest.tab";
	
	var TabItem = function(options){
		$.extend(this,{
			canClose:true,
			isHide:false
		},options);
	}
	
	var Tab = function(element,options){
		this.$el = $(element);
		this.options = $.extend({},{
			tabBody:'.tab-content',
			maxTabCount:5
		},options);
		this.tabCount = 0;
		this.items = {};
	}
	
	Tab.prototype = {
		_getTabEl:function(item){
			var options = this.options;
			var html = '<li><a href="#'+item.code+'" data-toggle="tab">'+item.name;
			if(item.canClose == true){
				html = html + '<b class="close">&times;</b>';
			}
			html = html + '</a></li>';
			return html;
		},
		_getMoreTabEl:function(){
			var html = '<li class="dropdown"><a href="javascript:void(0);" class="dropdown-toggle" data-toggle="dropdown">更多<b class="caret"></b></a><ul class="dropdown-menu"></ul></li>';
			return html;
		},
		_getMoreItemEl:function(item){
			var html = '<li><a href="javascript:void(0);">'+item.name+'</a></li>';
			return html;
		},
		_getTabBodyEl:function(item){
			return '<div class="tab-pane" id="'+item.code+'"><div class="alert alert-warning fade in" style="width:30%;margin:auto;text-align:center;">加载数据中......</div></div>';
		},
		_hideTab2MoreItem:function(){
			for(var key in this.items){
				var item = this.items[key];
				if(item.isHide == false && !item.$el.hasClass('active')){
					item.isHide = true;
					item.$el.hide();
					if(item.$moreItem){
						item.$moreItem.show();
					}else{
						var self = this;
						item.$moreItem = $(this._getMoreItemEl(item)).appendTo(this.$moreTab.find('.dropdown-menu'));
						item.$moreItem.find('a').click(function(){
							self._hideTab2MoreItem();
							item.isHide = false;
							item.$moreItem.hide();
							item.$el.show();
							item.$el.find('[data-toggle="tab"]').click();
						})
					}
					return true;
				}
			}
			return false;
		},
		_hideMoreItem2Tab:function(){
			for(var key in this.items){
				var temp = this.items[key];
				if(temp.isHide == true){
					temp.$el.show();
					temp.isHide = false;
					temp.$moreItem.hide();
					return true;
				}
			}
			return false;
		},
		_loadContent:function(item){
			if(item.url){
				$.get(item.url,{},function(result){
					$('#'+item.code).html(result);
				})
			}
		},
		addTab:function(options){
			var item = new TabItem(options);
			if(item.code){
				var temp = this.items[item.code];
				if(temp){
					if(temp.isHide == true ){
						if(this.tabCount >= this.options.maxTabCount){
							this._hideTab2MoreItem();
						}
						temp.$el.show();
						temp.isHide = false;
						temp.$moreItem.hide();
					}
					temp.$el.find('[data-toggle="tab"]').click();
				}else{
					if(this.tabCount >= this.options.maxTabCount){
						if(!this.$moreTab){
							this.$moreTab = $(this._getMoreTabEl()).appendTo(this.$el);
						}else{
							this.$moreTab.show();
						}
						this._hideTab2MoreItem();
					}
					this.items[item.code] = item;
					this.tabCount++;
					var self = this;
					$(this._getTabBodyEl(item)).appendTo($(this.options.tabBody));
					item.$el = $(this._getTabEl(item));
					if(this.$moreTab){
						item.$el.insertBefore(this.$moreTab);
					}else{
						item.$el.appendTo(this.$el);
					}
					item.$el.find('.close').click(function(){
						self.removeTab(item.code);
					});
					item.$el.find('[data-toggle="tab"]').click();
					this._loadContent(item);
				}
			}
		},
		removeTab:function(code){
			var item = this.items[code];
			if(item){
				var isActive = item.$el.hasClass('active');
				item.$el.remove();
				if(item.$moreItem){item.$moreItem.remove();}
				$('#'+code).remove();
				item = null;
				delete this.items[code];
				this.tabCount--;
				if(this.tabCount > this.options.maxTabCount){
					this._hideMoreItem2Tab();
				}else if(this.$moreTab){
					this.$moreTab.hide();
				}
				if(isActive){
					for(var key in this.items){
						var temp = this.items[key];
						if(temp.isHide == false){
							temp.$el.find('[data-toggle="tab"]').click();
							break;
						}
					}
				}
			}
		}
	}
	
	$.fn.jfTab = function(){
		var args = Array.prototype.slice.call(arguments, 0);
		var value = undefined;
		this.each(function () {
			var $this = $(this);
			//throw 'only support ul element'
			if (args.length === 0 || typeof(args[0]) === "object") {
				opts = args.length === 0 ? {} : $.extend({}, args[0]);
				$this.data(KEY,new Tab(this,opts)); //创建控件
			}else if (typeof(args[0]) === "string"){
				var entity = $this.data(KEY);
                if(entity === undefined){return;}
                value = entity[args[0]].apply(entity, args.slice(1));
			}else{
				 throw 'Invalid arguments to '+KEY+' plugin: ' + args;
			}
	    });
	    return (value === undefined) ? this : value;
	};
	
	$.fn.jfTab.Constructor = Tab;
})(window.jQuery)